﻿package com.farm.code.web.action;

import com.farm.code.domain.FarmCodeType;
import com.farm.code.server.FarmCodeTypeManagerInter;

import com.farm.web.easyui.EasyUiTreeNode;
import com.farm.web.easyui.EasyUiUtils;

import java.util.List;
import java.util.Map;
import com.farm.core.page.CommitType;
import com.farm.core.page.PageSet;
import com.farm.core.page.PageType;
import com.farm.core.sql.query.DataQuery;
import com.farm.core.sql.result.DataResult;
import com.farm.web.WebSupport;
import com.farm.web.spring.BeanFactory;

/**
 * 工程类型
 * 
 * @author autoCode
 * 
 */

public class ActionFarmCodeTypeQuery extends WebSupport {
	private Map<String, Object> jsonResult;// 结果集合
	private DataQuery query;// 条件查询
	private FarmCodeType entity;// 实体封装
	private PageSet pageset;// 请求状态
	private String ids;// 主键集合
	private String parentId;
	private String parentName;
	private String id;// 异步树传入的id
	private List<EasyUiTreeNode> treeNodes;// 异步树的封装

	/**
	 * 查询结果集合
	 * 
	 * @return
	 */
	public String queryall() {
		// TODO 自动生成代码,修改后请去除本注释
		try {
			query = EasyUiUtils.formatGridQuery(getRequest(), query);
			DataResult result = aloneIMP.createFarmCodeTypeSimpleQuery(query)
					.search();
			jsonResult = EasyUiUtils.formatGridData(result);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return SUCCESS;
	}

	/**
	 * 提交修改数据
	 * 
	 * @return
	 */
	public String editSubmit() {
		// TODO 自动生成代码,修改后请去除本注释
		try {
			entity = aloneIMP.editFarmCodeTypeEntity(entity, getCurrentUser());
			pageset = new PageSet(PageType.UPDATE, CommitType.TRUE);
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.UPDATE,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	/**
	 * 提交新增数据
	 * 
	 * @return
	 */
	public String addSubmit() {
		// TODO 自动生成代码,修改后请去除本注释
		try {
			entity = aloneIMP
					.insertFarmCodeTypeEntity(entity, getCurrentUser());
			pageset = new PageSet(PageType.ADD, CommitType.TRUE);
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.ADD,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	/**
	 * 删除数据
	 * 
	 * @return
	 */
	public String delSubmit() {
		// TODO 自动生成代码,修改后请去除本注释
		try {
			for (String id : parseIds(ids)) {
				aloneIMP.deleteFarmCodeTypeEntity(id, getCurrentUser());
			}
			pageset = new PageSet(PageType.ADD, CommitType.TRUE);
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.DEL,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	/**
	 * 跳转
	 * 
	 * @return
	 */
	public String forSend() {
		// TODO 自动生成代码,修改后请去除本注释
		return SUCCESS;
	}

	/**
	 * 显示详细信息（修改或浏览时）
	 * 
	 * @return
	 */
	public String view() {
		try {
			if (entity != null && entity.getParentid() != null) {
				parentId = entity.getParentid();
			}
			if (parentId != null) {
				if (aloneIMP.getFarmCodeTypeEntity(parentId) != null) {
					parentName = aloneIMP.getFarmCodeTypeEntity(parentId)
							.getName();
				} else {
					parentId = null;
				}
			}
			switch (pageset.getPageType()) {
			case (1): {// 新增
				return SUCCESS;
			}
			case (0): {// 展示
				entity = aloneIMP.getFarmCodeTypeEntity(ids);
				return SUCCESS;
			}
			case (2): {// 修改
				entity = aloneIMP.getFarmCodeTypeEntity(ids);
				return SUCCESS;
			}
			default:
				break;
			}
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.OTHER,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	public String loadTreeNode() {
		if (id == null) {
			id = "NONE";
		}
		treeNodes = EasyUiTreeNode.formatAsyncAjaxTree(EasyUiTreeNode
				.queryTreeNodeOne(id, "SORT", "farm_code_type", "ID",
						"PARENTID", "NAME", "CTIME").getResultList(),
				EasyUiTreeNode.queryTreeNodeTow(id, "SORT", "farm_code_type",
						"ID", "PARENTID", "NAME", "CTIME").getResultList(),
				"PARENTID", "ID", "NAME", "CTIME");
		return SUCCESS;
	}

	private final static FarmCodeTypeManagerInter aloneIMP = (FarmCodeTypeManagerInter) BeanFactory
			.getBean("farm_code_typeProxyId");

	// ----------------------------------------------------------------------------------
	public DataQuery getQuery() {
		return query;
	}

	public void setQuery(DataQuery query) {
		this.query = query;
	}

	public FarmCodeType getEntity() {
		return entity;
	}

	public void setEntity(FarmCodeType entity) {
		this.entity = entity;
	}

	public PageSet getPageset() {
		return pageset;
	}

	public void setPageset(PageSet pageset) {
		this.pageset = pageset;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public Map<String, Object> getJsonResult() {
		return jsonResult;
	}

	public void setJsonResult(Map<String, Object> jsonResult) {
		this.jsonResult = jsonResult;
	}

	public String getParentId() {
		return parentId;
	}

	public String getParentName() {
		return parentName;
	}

	public void setParentName(String parentName) {
		this.parentName = parentName;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public List<EasyUiTreeNode> getTreeNodes() {
		return treeNodes;
	}

	public void setTreeNodes(List<EasyUiTreeNode> treeNodes) {
		this.treeNodes = treeNodes;
	}

//	private static final Logger log = Logger
//			.getLogger(ActionFarmCodeTypeQuery.class);
	private static final long serialVersionUID = 1L;

}
